package com.example.system.class08;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 * @Date 2022/7/25
 * @Created by Jonathan
 * T是非基础类型
 */
public class HeapCreator<T> {

    private List<T> heap;
    // [a,b,c]
    // a -> 0
    // b -> 1
    // c -> 2
    private HashMap<T, Integer> indexMap;
    private int headSize;
    private Comparator<? extends T> comp;

    public HeapCreator(Comparator<? extends T> comp) {
        heap = new ArrayList<>();
        indexMap = new HashMap<>();
        headSize = 0;
        this.comp = comp;
    }

    public int size() {
        return headSize;
    }

    public boolean isEmpty() {
        return headSize == 0;
    }

    public boolean contains(T obj) {
        return indexMap.containsKey(obj);
    }

    public T peek() {
        return heap.get(0);
    }

    public void push(T obj) {
        heap.add(obj);
        indexMap.put(obj, headSize);
        heapInsert(headSize++);
    }

    private void heapInsert(int index) {
        /*while (comp.compare(heap.get(index), heap.get((index - 1) / 2)) < 0) {
            swap(index, (index - 1) / 2);
            index = (index - 1) / 2;
        }*/
    }


    public T pop() {
        T ans = heap.get(0);
        swap(0, headSize - 1);
        indexMap.remove(ans);
        heap.remove(--headSize);
        // 往下调整
        heapify(0);
        return ans;
    }


    public void resign(T obj) {
        heapInsert(indexMap.get(obj));
        heapify(indexMap.get(obj));
    }

    public List<T> getAllElement() {
        List<T> ans = new ArrayList<>();
        for (T t : heap) {
            ans.add(t);
        }
        return ans;
    }

    private void heapify(int i) {

    }

    private void swap(int i, int j) {
        // 同时交换
    }
}

